package com.lingdian.demo.databind;

import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/7/23.
 */
@Controller
public class DogController {

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    @RequestMapping("/dog1")
    @JsonView(Dog.WithoutAge.class)
    @ResponseBody
    public Dog getDog(){
        Dog dog = new Dog();
        dog.setAddress("西安");
        dog.setAge(10);
        dog.setBirth(new Date());
        dog.setName("小熊");
        dog.setType("熊类");
        return  dog;
    }
    @RequestMapping("/dogForm")
    @ResponseBody
    public Dog getDog2(Dog dog ){
        return dog;
    }
    @RequestMapping("/dogJson")
    @ResponseBody
    public Dog getDog3(@RequestBody Dog dog ){
        return dog;
    }

}
